<template>
	<view>
		<back :showBackText="false" customClass="bg-gradual-blue text-white" title="我的收藏"></back>
		<view class="flex flex-direction" style="min-height: 100vh;">
			<view class="flex-sub" style="margin-bottom: 270rpx;">
				<scroll-view scroll-x class="bg-white nav text-center top">
					<view class="cu-item" :class="index==selectModelIndex?'text-blue cur':''"
						v-for="(item,index) in selectModel" :key="index" @tap="selectModelTap" :data-id="index">
						{{item}}
					</view>
				</scroll-view>
				<view class="margin-top-xs" v-if="isLoad == true">
					<view @tap="courseTap" class="course-layout margin-bottom-xs" :data-item="course"
						v-for="(course, idx) in listData" :key="idx">
						<view class="name-layout">
							<text>{{ course.name }}</text>
							<courselabel :code="course.code" :topicCount="course.count ? '共'+course.count+'题' :''">
							</courselabel>
						</view>
						<image mode="aspectFit" src="/static/img/ic_arrow.png"></image>
					</view>
				</view>
				<empty v-if="isLoad == true && listData.length == 0"></empty>
			</view>
			<adfootbanner></adfootbanner>
		</view>
	</view>
</template>

<script>
	let app = getApp();
	let that = null;
	export default {
		data() {
			return {
				isLoad: false,
				course_id: 0,
				isAllScreen: app.globalData.isAllScreen,
				listData: [],
				selectModel: ['收藏题目', '收藏课程'],
				selectModelIndex: 0,
			};
		},
		onLoad(options) {
			that = this;
			const {
				course_id = 0
			} = options || {};
			that.course_id = course_id;
		},
		onShow: function() {
			that.getColl();
		},
		methods: {
			getColl: function(e) {
				app.globalData.server
					.getRequest('user/collect', {
						type: that.selectModelIndex,
						course_id: that.course_id
					})
					.then(function(t) {
						that.setData({
							isLoad: true,
							listData: t.data
						});
					})
					.catch(function(a) {
						app.showToast('获取收藏列表失败');
					});
			},
			selectModelTap: function(options) {
				that.setData({
					isLoad: false,
					selectModelIndex: options.currentTarget.dataset.id
				});
				that.getColl();
			},
			courseTap: function(options) {
				let url = '';
				let item = options.currentTarget.dataset.item;
				console.log(item)
				if (that.selectModelIndex == 0) {
					//收藏题chargeType=0
					url = '../topic/topic?mainType=2' + '&title=【收藏】' + item.name + '&id=' + item.course_id +
						'&chargeType=0';
				} else {
					url = '/pages/practice/course/detail?id=' + item.course_id
				}
				uni.navigateTo({
					url: url
				});
			}
		}
	};
</script>
<style>
	@import './coll.css';
</style>